package com.comment.service.impl;

import com.comment.dto.*;
import com.comment.entity.CommentInfo;
import com.comment.entity.CommentInfoExample;
import com.comment.entity.CommentReply;
import com.comment.entity.CommentReplyExample;
import com.comment.mapper.CommentInfoMapper;
import com.comment.mapper.CommentReplyMapper;
import com.comment.service.CommentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author vedeng
 * @className CommentServiceImpl
 * @description:
 * @date 2021/12/13 14:27
 */
@Service
public class CommentServiceImpl implements CommentService {


    @Autowired
    private CommentInfoMapper commentInfoMapper;


    @Autowired
    private CommentReplyMapper commentReplyMapper;

    @Override
    public int addComment(CommentRequest commentRequest) {
        CommentInfo commentInfo = new CommentInfo();
        commentInfo.setBlogId(commentRequest.getBlogId());
        commentInfo.setMemberId(commentRequest.getUserId());
        commentInfo.setMemberName(commentRequest.getUserName());
        commentInfo.setContent(commentRequest.getContent());
        commentInfo.setAddTime(new Date());
        commentInfoMapper.insertSelective(commentInfo);
        return 1;
    }


    @Override
    public List<CommentDTO> queryAllCommentByBlogId(CommentRequest commentRequest) {
        //Page<Object> page = PageHelper.startPage(commentRequest.getPageNum(), commentRequest.getPageSize());
        CommentInfoExample commentInfoExample = new CommentInfoExample();
        commentInfoExample.setOrderByClause("add_time desc");
        commentInfoExample.createCriteria().andBlogIdEqualTo(commentRequest.getBlogId()).andIsDelEqualTo(0);
        List<CommentInfo> commentInfos = commentInfoMapper.selectByExample(commentInfoExample);

        List<CommentDTO> commentDTOList = new ArrayList<>();

        commentInfos.stream().forEach(obj -> {
            CommentDTO commentDTO = new CommentDTO();
            commentDTO.setBlogId(obj.getBlogId());
            commentDTO.setContent(obj.getContent());
            commentDTO.setMemberId(obj.getMemberId());
            commentDTO.setMemberName(obj.getMemberName());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = formatter.format(obj.getAddTime());
            commentDTO.setAddTime(date);
            commentDTOList.add(commentDTO);
        });
        return commentDTOList;
    }




    @Override
    public void replyComment(CommentRequest commentRequest, int userId) {
        CommentReply commentReply = new CommentReply();

        commentReply.setCommentId(commentRequest.getCommentId());
        commentReply.setMemberId(userId);
        commentReply.setMemberName("评论人");
        commentReply.setReplyuserId(1);
        commentReply.setReplyuserName("被评论人名称");
        commentReply.setContent(commentRequest.getContent());
        commentReply.setAddTime(new Date());

        commentReplyMapper.insertSelective(commentReply);

    }




    @Override
    public CommentListDTO queryAllComment(CommentRequest commentRequest) {
        //先查询评论
        commentRequest.setBlogId(commentRequest.getBlogId());
        Page<Object> page = PageHelper.startPage(commentRequest.getPageNum(), commentRequest.getPageSize());
        CommentInfoExample commentInfoExample = new CommentInfoExample();
        commentInfoExample.setOrderByClause("add_time desc");
        commentInfoExample.createCriteria().andBlogIdEqualTo(commentRequest.getBlogId()).andIsDelEqualTo(0);
        List<CommentInfo> commentInfos = commentInfoMapper.selectByExample(commentInfoExample);
        CommentListDTO commentListDTO = new CommentListDTO();
        List<CommentDTO> commentDTOList = new ArrayList<>();

        commentInfos.stream().forEach(obj -> {
            CommentDTO commentDTO = new CommentDTO();
            commentDTO.setBlogId(obj.getBlogId());
            commentDTO.setContent(obj.getContent());
            commentDTO.setMemberId(obj.getMemberId());
            commentDTO.setMemberName("来自"+ obj.getMemberName() + "用户");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = formatter.format(obj.getAddTime());
            commentDTO.setAddTime(date);
            commentDTOList.add(commentDTO);
        });
        commentListDTO.setCommentDTOList(commentDTOList);
        return commentListDTO;
    }
}